/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package explorer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author user1
 */
public final class FileUnited {
    private String filePath;
    private String fileUri;
    private String fileContent;
    int separator, pathSeparator;
    
    public List<Integer> startChar = new ArrayList<Integer>();
    
    public FileUnited(File f) {
        filePath = f.getAbsolutePath();
        separator = filePath.lastIndexOf('.');
        pathSeparator = filePath.lastIndexOf(File.separatorChar);
        fileUri = f.toURI().toString();
        try {
            setContent(new FileReader(f));
        } catch (IOException e) {
            fileContent = e.toString();
        }
    }
    
    public FileUnited(FileUnited fu) {
        filePath = fu.getFilePath();
        fileUri = fu.getUri();
        separator = filePath.lastIndexOf('.');
        fileContent = fu.getContent();
        pathSeparator = filePath.lastIndexOf(File.separatorChar);
    }
    
    public void setStartChar(int i) {
        startChar.add(i);
    }
    
    
    public String getUri() {
        return fileUri;
    }
    
    public String getFilePath() {
        return filePath;
    }
    
    public String getExtension() {
        String result = filePath.substring(separator+1);
        if (result.isEmpty()) return "";
        else return result;
    }
    
    public String getName() {
        return filePath.substring(pathSeparator+1,separator);
    }
    
    public String getPath() {
        return filePath.substring(0,separator);
    }
    
    public String getFullPath() { 
        //return fileUri;
        return new StringBuffer(fileUri).insert(fileUri.indexOf('/'),"//").toString();
    }
    
    public String getContent() {
        return fileContent;
    }
    
    private void setContent(Reader f) throws IOException{
        BufferedReader br = new BufferedReader(f);
        StringBuilder result = new StringBuilder();
        String aLine;
        while ((aLine = br.readLine()) != null)
            result.append(aLine);
        fileContent = result.toString();
    }
    
    public static boolean fileValidator(File f) {
        String upperName = f.getName().toUpperCase();
        return (
                upperName.endsWith("TXT") ||
                upperName.endsWith("DOC") ||
                upperName.endsWith("DOCX")
                );
    }
}
